package www.openteam.com.model;

import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;
import www.openteam.com.api.UserManager;
import www.openteam.com.bean.Result;
import www.openteam.com.bean.User;

/**
 * Created by kylin on 2016/7/25.
 */
public class UserModelImpl implements  UserModel{


    private UserOnListener userOnListener;


    public UserModelImpl(UserOnListener userOnListener) {
        this.userOnListener = userOnListener;
    }

    /**
     * Rxandroid 使用
     * @param pageSize
     */
    @Override
    public void getUserData(int pageSize) {
        UserManager.getUserData(pageSize).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<User>() {
                    @Override
                    public void call(User u) {
                //  测试
//                     for (User.DataBean user:u.getData()
//                             ) {
//                            u.toString();
////                            System.out.println("1");
//                        }
                            userOnListener.onSuccess(u);
//
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        userOnListener.onFailure(throwable);
                    }
                });



    }

        @Override
        public void login(String name, String pass) {
            UserManager.login(name,pass).subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new Action1<Result>() {
                        @Override
                        public void call(Result result) {
                            userOnListener.onLoginSuccess(result);
                        }
                    } ,new Action1<Throwable>() {
                            @Override
                            public void call(Throwable throwable) {
                                userOnListener.onLoginFailure(throwable);
                            }
                    });
        }

    /**
     * 使用这个接口连接 Presenter层
     */
    public interface UserOnListener{
        void onSuccess(User u);
        void onLoginSuccess(Result result);
        void onLoginFailure(Throwable e);
        void onFailure(Throwable e);
    }
}
